package com.tanhua.manage.listener;

import com.tanhua.commons.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 动态审核消息监听器
 */
@Component
@RocketMQMessageListener(topic = "tanhua-publish", consumerGroup = "tanhua-publish-consumer")
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String message) {
        // 1.根据动态ID查询动态内容
        Publish publish = publishApi.findById(message);

        // 2.审核动态内容
        if (publish != null && publish.getState() == 0) {

            Integer state = publish.getState();

            // 2.1 审核文字
            boolean textContentCheck = huaWeiUGCTemplate.textContentCheck(publish.getTextContent());
            if (textContentCheck) {
                // 2.2 审核文字成功后，再审核图片
                boolean imageContentCheck = huaWeiUGCTemplate.imageContentCheck(publish.getMedias().toArray(new String[]{}));

                if (imageContentCheck) {
                    state = 1; // 审核通过
                } else {
                    state = 2; // 审核不通过
                }
            } else {
                state = 2; // 审核不通过
            }

            // 2.3 调用服务提供者更新动态的状态
            publishApi.updateState(message, state);
        }
    }
}
